package cn.tkxqc.sell.service.impl;

import cn.tkxqc.sell.dataobject.OrderDetail;
import cn.tkxqc.sell.dataobject.OrderMaster;
import cn.tkxqc.sell.dataobject.ProductInfo;
import cn.tkxqc.sell.dto.CartDto;
import cn.tkxqc.sell.dto.OrderDTO;
import cn.tkxqc.sell.enums.OrderStatusEnum;
import cn.tkxqc.sell.enums.PayStatusEnum;
import cn.tkxqc.sell.enums.ResultEnum;
import cn.tkxqc.sell.exception.SellException;
import cn.tkxqc.sell.respository.OrderDetailRepository;
import cn.tkxqc.sell.respository.OrderMasterRepository;
import cn.tkxqc.sell.service.OrderService;
import cn.tkxqc.sell.service.ProductService;
import cn.tkxqc.sell.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Override
    @Transactional
    public OrderDTO create(OrderDTO orderDTO) {

        String orderId = KeyUtil.genUniqueKey(); //创建的订单id
        BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);

//        List<CartDto> cartDtoList = new ArrayList<>();

//        1.查询商品（数量、价格）
        for (OrderDetail orderDetail:orderDTO.getOrderDetailList()) {
            ProductInfo productInfo = productService.findOne(orderDetail.getProductId());
            if (productInfo == null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXSIT);
            }
            // 2.计算总价
            orderAmount = productInfo.getProductPrice().
                    multiply(new BigDecimal(orderDetail.getProductQuantity())).
                    add(orderAmount);
            //订单详情入库
            orderDetail.setDetailId(KeyUtil.genUniqueKey());
            orderDetail.setOrderId(orderId);
            BeanUtils.copyProperties(productInfo,orderDetail);
            orderDetailRepository.save(orderDetail);

//            CartDto cartDto = new CartDto(orderDTO.getProductId(),orderDTO.getProductQuantity());
//            cartDtoList.add(cartDto);
        }


        // 3.写入订单数据（OrderMaster和OrderDetail）
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDTO,orderMaster);
        orderMaster.setOrderId(orderId);
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());

        orderMasterRepository.save(orderMaster);

        // 4.扣库存
        List<CartDto> cartDtoList = orderDTO.getOrderDetailList().stream().map(e ->
            new CartDto(e.getProductId(),e.getProductQuantity())).collect(Collectors.toList());

        productService.decreaseStock(cartDtoList);

        return orderDTO;
    }

    @Override
    public OrderDTO findOne(String orderId) {
        return null;
    }

    @Override
    public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) {
        return null;
    }

    @Override
    public OrderDTO cancel(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public OrderDTO finish(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public OrderDTO paid(OrderDTO orderDTO) {
        return null;
    }
}
